package com.gj.demojuc.chap09;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 不具备原子性
 * 可以加锁synchronize和lock
 * 也可以用CAS 比较并交换Compare and swap 修改数据是先去判断期望值和原值进行比较，
 * 如果一致则修改成新值，不一致则重新执行CAS
 *
 */

class DataOne{
    private AtomicInteger number = new AtomicInteger(0);
    public Integer increment(){
        int i = number.incrementAndGet();
        return i;
    }
}
public class VolatileAtomicDemo {
    public static void main(String[] args) {
        DataOne dataOne = new DataOne();
        //1000个线程并发
        for (int i = 0; i < 1000; i++) {
            new Thread(()->{
                System.out.println(dataOne.increment());
            }).start();
        }
    }
}
